import { Injectable } from '@angular/core';
import { I18NService } from './i18n.service';
import { HttpErrorResponse, HttpResponse } from '@angular/common/http';
import { TimeoutError } from 'rxjs';
import { MessageService } from '@iux/live';

@Injectable({
  providedIn: 'root'
})
export class ExceptionService {
  constructor(private i18n: I18NService, private messageService: MessageService) {}

  isException(res: HttpResponse<any>) {
    return false;
  }

  doException(error) {
    // http错误
    if (error instanceof HttpErrorResponse) {
      console.log('httpError--->>>', error);
      this.messageService.error(this.i18n.get('common_exception'));
    }
    // 超时错误
    else if (error instanceof TimeoutError) {
      console.log('TimeoutError--->>>', error);
      this.messageService.error(this.i18n.get('common_timeout'));
    }
    // 其他错误
    else {
      console.log('OtherError--->>>', error);
      this.messageService.error(this.i18n.get('common_exception'));
    }
  }
}
